<template>
  <el-dialog :title="$t('ota.upgradeTask.detail.name')" :visible.sync="dialogVisible"
             @close="cancel" append-to-body custom-class='deviceDialog' width="1500px">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="118px">

      <el-form-item :label="$t('ota.upgradeTask.detail.deviceCode')" prop="deviceCode">
        <el-input v-model="queryParams.deviceCode" :placeholder="$t('ota.upgradeTask.detail.placeholder.deviceCode')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item :label="$t('ota.upgradeTask.detail.updateState')" prop="updateState">
          <el-select v-model="queryParams.updateState"
                     clearable size="middle" style="width: 240px">
            <el-option v-for="dict in dict.type.upgrade_status"
                       :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
      </el-form-item>

      <el-form-item :label="$t('ota.upgradeTask.detail.versionType')" prop="versionType">
          <el-select v-model="queryParams.versionType" :placeholder="$t('ota.version.placeholder.versionType')" style="width: 240px" clearable>
              <el-option v-for="dict in dict.type.firmware_type" :key="dict.value" :label="dict.label" :value="dict.value" />
      </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.button.query')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.button.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['ota:upgradeTask:detail']">{{$t('common.button.export')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="firmwareUpdateRecordList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('ota.upgradeTask.planName')" align="center" prop="planName" width="180" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('ota.upgrade.deviceCode')" align="center" prop="deviceCode" width="180" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('ota.version.versionNo')" align="center" prop="versionNo" width="180" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('ota.version.versionType')" align="center" prop="versionType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.firmware_type" :value="scope.row.versionType" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.detail.downloadProgress')" align="center" prop="downloadProgress" width="88" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.downloadProgress">{{ scope.row.downloadProgress + '%'}}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.detail.updateState')" align="center" prop="updateState" width="150" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.upgrade_status" :value="scope.row.updateState" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.detail.lastInstructTime')" align="center" prop="lastInstructTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastInstructTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.detail.startTime')" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.detail.endTime')" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('ota.upgradeTask.detail.versionNoBefore')" align="center" prop="versionNoBefore" width="150"/>
      <el-table-column :label="$t('ota.upgradeTask.detail.versionNoAfter')" align="center" prop="versionNoAfter" width="150"/>
      <el-table-column :label="$t('ota.upgradeTask.detail.failCount')" align="center" prop="failCount" width="150"/>
      <el-table-column :label="$t('ota.upgradeTask.detail.failMessage')" align="center" prop="failMessage" width="220" :show-overflow-tooltip="true"/>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

  </el-dialog>
</template>

<script>
import { listFirmwareUpdateRecord } from "@/api/ota/upgradeTask";

export default {
  name: "Details",
  dicts: ['firmware_type', 'upgrade_status'],
  data() {
    return {
      dialogVisible: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备固件版本升级记录表格数据
      firmwareUpdateRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        updatePlanId: undefined,
        deviceCode: undefined,
        versionType: undefined,
        updateState: undefined,
        versionNoBefore: undefined,
        versionNo: undefined,
      },
    };
  },

  methods: {
    init(updatePlanId) {
      this.queryParams.updatePlanId = updatePlanId
      this.dialogVisible = true
      this.getList();
    },
    cancel() {
      this.dialogVisible = false
    },
    /** 查询设备固件版本升级记录列表 */
    getList() {
      this.loading = true;
      listFirmwareUpdateRecord(this.queryParams).then(response => {
        this.firmwareUpdateRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/upgrade/record/export', {
        ...this.queryParams
      }, `firmwareUpdateRecord_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>

</style>
